﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using Srvtools;

namespace CXCLR
{
    public partial class FormXCLR : InfoForm
    {
        public FormXCLR()
        {
            InitializeComponent();

        }

        private void grdSCBOM_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void infoStatusStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void FormXCLR_Load(object sender, EventArgs e)
        {
            showFrom2();
            ////infoPanelSCGX.Visible = true;
            //idGXZLS.SetWhere("USERID='" + CliUtils.fLoginUser + "'");
            //idGXZLS.Active = true;    
             }

        private void infoDataGridView1_DoubleClick(object sender, EventArgs e)
        {
            //infoPanelSCGX.Visible = false;
            DataSet ds = idTEM.Execute("EXEC PTY '" + ((DataRowView)ibsGXZLS.Current)["LBMC"].ToString() + "','" + ((DataRowView)ibsGXZLS.Current)["GXMC"].ToString() + "','" + ((DataRowView)ibsGXZLS.Current)["XBMC"].ToString() + "','" + CliUtils.fLoginUser + "','" + ((DataRowView)ibsGXZLS.Current)["GXBH"].ToString() + "','XCLR'");
            //EXEC PTY &(GXZLS->LBMC),&(GXZLS->GXMC),&(GXZLS->XBMC),&(_USER_NAME),&(GXZLS->GXBH),&(LB)
            if (ds.Tables[0].Rows.Count > 0)
                idView.SetWhere("SCBH='" + ds.Tables[0].Rows[0][0].ToString() + "'");

        }

        private void button1_Click(object sender, EventArgs e)
        {

            showFrom2();
        }

        private void showFrom2()
        {

            Form2 frm = new Form2();
            frm.ShowDialog();
            DataRowView dr = (DataRowView)frm.ibsGXZLS.Current;

            DataSet ds = idTEM.Execute("EXEC PTY '" + dr["LBMC"].ToString() + "','" + dr["GXMC"].ToString() + "','" + dr["XBMC"].ToString() + "','" + CliUtils.fLoginUser + "','" + dr["GXBH"].ToString() + "','XCLR_XZ'");
            //EXEC PTY &(GXZLS->LBMC),&(GXZLS->GXMC),&(GXZLS->XBMC),&(_USER_NAME),&(GXZLS->GXBH),&(LB)
            if (ds.Tables[0].Rows.Count > 0)
                idView.SetWhere("SCBH='" + ds.Tables[0].Rows[0][0].ToString() + "'");
        }

        private void grdView_Click(object sender, EventArgs e)
        {
            DataRowView drv = (DataRowView)ibsSCZL.Current;
            if (drv != null)
            {
                string SCBH = drv["SCBH"].ToString();
                idSCZL.SetWhere(" SCBH = '" + SCBH + "'");
            }
        }

        private void bindingNavigatorAddNewItem1_Click(object sender, EventArgs e)
        {
            LBInfoTextBox.Enabled = false;
        }

        private void bindingNavigatorEditItem1_Click(object sender, EventArgs e)
        {
            LBInfoTextBox.Enabled = true;
        }

        private void infoDataGridView1_Click(object sender, EventArgs e)
        {
            DataRowView drv = (DataRowView)ibsElseSet.Current;
            string MC = drv["MC"].ToString();
            string S1 = drv["S1"].ToString();
            string whereStr = string.Format(" LBMC = '{0}' AND LB = '{1}'", MC, S1);
            idGXZL.SetWhere(whereStr);

        }

        private void bindingNavigatorPrintItem1_Click(object sender, EventArgs e)
        {
            Form1 frm = new Form1();
            frm.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("NewDataSet_SCBOM", idSCZL.RealDataSet.Tables[1]));
            frm.reportViewer1.RefreshReport();
            frm.ShowDialog();

            System.Drawing.Image image;
            int width = 148, height = 55;
            string fileSavePath = AppDomain.CurrentDomain.BaseDirectory + "BarcodePattern.jpg";
            if (File.Exists(fileSavePath))
                File.Delete(fileSavePath);
            GetBarcode(height, width, BarcodeLib.TYPE.CODE128, "20131025-136", out image, fileSavePath);

            pictureBox1.Image = Image.FromFile("BarcodePattern.jpg");
        }


        public static void GetBarcode(int height, int width, BarcodeLib.TYPE type, string code, out System.Drawing.Image image, string fileSaveUrl)
        {
            try
            {
                image = null;
                BarcodeLib.Barcode b = new BarcodeLib.Barcode();
                b.BackColor = System.Drawing.Color.White;//图片背景颜色  
                b.ForeColor = System.Drawing.Color.Black;//条码颜色  
                b.IncludeLabel = true;
                b.Alignment = BarcodeLib.AlignmentPositions.LEFT;
                b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER;
                b.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;//图片格式  
                System.Drawing.Font font = new System.Drawing.Font("verdana", 10f);//字体设置  
                b.LabelFont = font;
                b.Height = height;//图片高度设置(px单位)  
                b.Width = width;//图片宽度设置(px单位)  

                image = b.Encode(type, code);//生成图片  
                image.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);

            }
            catch (Exception ex)
            {

                image = null;
            }
        }


    }
}

